import smtplib
from email.mime.text import MIMEText
from email.header import Header


def send_email():
    # 配置参数
    smtp_server = "smtp.163.com"
    smtp_port = 465  # SSL 端口
    sender_email = "chai0304@163.com"
    sender_auth_code = "MWxz6YHFrvAGdqu2"  # 授权码，非登录密码
    receiver_email = "chai0304@163.com"  # 支持任意邮箱

    # 构建邮件内容
    subject = "自动化测试报告"
    content = "这是测试报告内容"

    msg = MIMEText(content, "plain", "utf-8")
    msg["From"] = Header(f"发件人名称 <{sender_email}>")
    msg["To"] = Header(f"收件人名称 <{receiver_email}>")
    msg["Subject"] = Header(subject, "utf-8")

    # 发送邮件
    try:
        server = smtplib.SMTP_SSL(smtp_server, smtp_port)
        server.login(sender_email, sender_auth_code)
        server.sendmail(sender_email, [receiver_email], msg.as_string())
        print("邮件发送成功")
    except Exception as e:
        print(f"发送失败: {str(e)}")
    finally:
        server.quit()
